package ajk.wyy.com.htmltopicturedemo;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.Utils;

import java.io.File;

import ajk.wyy.com.htmltopicturedemo.constant.Constant;

public class MainActivity extends AppCompatActivity {

    private TextView sign;
    private WebView webview;

    public static String path1 = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "ls.png";

    boolean clearHistory = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 锁定竖屏
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        Utils.init(this);
        initView();
        initData();
        initListener();
    }

    private void initListener() {
        sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(MainActivity.this, LandscapeActivity.class), 1);
            }
        });
    }

    private void initData() {
        webview.loadUrl("http://www.youehu.com/hetong.html");

    }

    private void initView() {
        sign = (TextView) findViewById(R.id.sign);
        webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webview.getSettings().setAppCacheEnabled(true);  //开启缓存功能
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 100) {
        } else if (resultCode == 101) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 2;

//            Bitmap bm = BitmapFactory.decodeFile(path1, options);
//            Uri uri = getImageContentUri(this, new File(path1));
            String html = Constant.HTML;
            String htmls = html.replace("qianming", path1);
            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "ls.html");

            FileIOUtils.writeFileFromString(file, htmls);
            webview.loadUrl("file://" + file.getAbsolutePath());
            webview.clearHistory();
        }

    }

    public Uri getImageContentUri(Context context, File imageFile) {
        Uri uri;
        //如-系统版本>=7.0，用第一种方式获取uri
        if (Build.VERSION.SDK_INT >= 24) {

            //兼容系统6.0,7.0
            String filePath = imageFile.getAbsolutePath();
            Cursor cursor = context.getContentResolver().query(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    new String[]{MediaStore.Images.Media._ID},
                    MediaStore.Images.Media.DATA + "=? ",
                    new String[]{filePath}, null);
            if (cursor != null && cursor.moveToFirst()) {//
                int id = cursor.getInt(cursor
                        .getColumnIndex(MediaStore.MediaColumns._ID));

                Uri baseUri = Uri.parse("content://media/external/images/media");
                cursor.close();
                uri = Uri.withAppendedPath(baseUri, "" + id);
            } else {
                if (imageFile.exists()) {
                    ContentValues values = new ContentValues();
                    values.put(MediaStore.Images.Media.DATA, filePath);
                    uri = context.getContentResolver().insert(
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                } else {
                    uri = null;
                }
            }
        } else {
            //系统7.0以下
            uri = Uri.fromFile(imageFile);
        }
        if (uri == null)
            uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", imageFile);
        return uri;
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
}
